//package testdir;
//
///**
// * Created by Aivin on 2017/12/29.
// */
//public class TestOldSrc
//{
//
//
//    private void sendRequestOptions() throws IOException {
//        String request = "OPTIONS rtsp://" + mParams.address + " RTSP/1.0\r\n" + addHeaders();
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        mOutputStream.write(request.getBytes("UTF-8"));
//        Response.parseResponse(mBufferreader);
//    }
//
//    private void sendRequestDescribe() throws IOException {
//        String request = "DESCRIBE rtsp://" + mParams.address + " RTSP/1.0\r\n" + addHeaders();
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        Describeflag = true;
//        mOutputStream.write(request.getBytes("UTF-8"));
//        Response.parseResponse(mBufferreader);
//    }
//
//    private void sendRequestSetup() throws IOException {
//        Matcher matcher;
//        String request;
//        if ( isTCPtranslate ) {
//            request = "SETUP rtsp://" + mParams.address + "/" + sdpInfo.videoTrack + " RTSP/1.0\r\n"
//                    + "Transport: RTP/AVP/TCP;unicast;client_port=55640-55641" + "\r\n"
//                    + addHeaders();
//        } else {
//            request = "SETUP rtsp://" + mParams.address + "/" + sdpInfo.videoTrack +" RTSP/1.0\r\n"
//                    + "Transport: RTP/AVP/UDP;unicast;client_port=55640-55641" + "\r\n"
//                    + addHeaders();
//        }
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        mOutputStream.write(request.getBytes("UTF-8"));
//        Response mResponse = Response.parseResponse(mBufferreader);
//
//        //there has two different session type, one is without timeout , another is with timeout
//        matcher = Response.regexSessionWithTimeout.matcher(mResponse.headers.get("session"));
//        if(matcher.find())  mSession = matcher.group(1);
//        else mSession = mResponse.headers.get("session");
//        Log.d(tag,"the session is " + mSession);
//
//        //get the port information and start the RTP socket, ready to receive data
//        if(isTCPtranslate) matcher = Response.regexTCPTransport.matcher(mResponse.headers.get("transport"));
//        else matcher = Response.regexUDPTransport.matcher(mResponse.headers.get("transport"));
//        if(matcher.find()) {
//            Log.d(tag, "The client port is:" + matcher.group(1) + " ,the server prot is:" + (isTCPtranslate?"null":matcher.group(2)) + "...");
//            mParams.rtpPort = Integer.parseInt(matcher.group(1));
//            if(!isTCPtranslate) mParams.serverPort = Integer.parseInt(matcher.group(2));
//
//            //prepare for the video decoder
//            mH264Stream = new H264Stream(sdpInfo);
//            mH264Stream.setSurfaceView(mSurfaceView);
//
//            if(isTCPtranslate) mRtpSocket = new RtpSocket(isTCPtranslate, mParams.rtpPort, mParams.host, -1,TRACK_VIDEO);
//            else mRtpSocket = new RtpSocket(isTCPtranslate, mParams.rtpPort, mParams.host, mParams.serverPort,TRACK_VIDEO);
//            mRtpSocket.startRtpSocket();
//            mRtpSocket.setStream(mH264Stream);
//        } else {
//            if(isTCPtranslate) {
//                Log.d(tag,"Without get the transport port infom, use the rtsp tcp socket!");
//                mParams.rtpPort = mParams.port;
//
//                //prepare for the video decoder
//                mH264Stream = new H264Stream(sdpInfo);
//                mH264Stream.setSurfaceView(mSurfaceView);
//
//                mRtpSocket = new RtpSocket(isTCPtranslate,mParams.rtpPort,mParams.host,-2,TRACK_VIDEO);
//                mRtpSocket.setRtspSocket(mSocket);
//                mRtpSocket.startRtpSocket();
//                mRtpSocket.setStream(mH264Stream);
//                mState = STATE_STARTED;
//            }
//        }
//    }
//
//    private void sendRequestPlay() throws IOException {
//        String request = "PLAY rtsp://" + mParams.address + " RTSP/1.0\r\n"
//                + "Range: npt=0.000-\r\n"
//                + addHeaders();
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        mOutputStream.write(request.getBytes("UTF-8"));
//        Response.parseResponse(mBufferreader);
//    }
//
//    private void sendRequestTeardown() throws IOException {
//        String request = "TEARDOWN rtsp://" + mParams.address + "/" + sdpInfo.videoTrack + " RTSP/1.0\r\n" + addHeaders();
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        mOutputStream.write(request.getBytes("UTF-8"));
//        mState = STATE_STOPPING;
//    }
//
//    private void sendRequestGetParameter() throws IOException {
//        String request = "GET_PARAMETER rtsp://" + mParams.address + "/" + sdpInfo.videoTrack + " RTSP/1.0\r\n" + addHeaders();
//        Log.d(tag, request.substring(0, request.indexOf("\r\n")));
//        mOutputStream.write(request.getBytes("UTF-8"));
//    }
//
//    private String addHeaders() {
//        return "CSeq: " + (++CSeq) + "\r\n"
//                + ((authorBase64 == null)?"":("Authorization: Basic " +authorBase64 +"\r\n"))
//                + "User-Agent: " + UserAgent + "\r\n"
//                + ((mSession == null)?"":("Session: " + mSession + "\r\n"))
//                + "\r\n";
//    }
//
//    static class Response {
//
//        public static final Pattern regexStatus = Pattern.compile("RTSP/\\d.\\d (\\d+) .+",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexHeader = Pattern.compile("(\\S+): (.+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexUDPTransport = Pattern.compile("client_port=(\\d+)-\\d+;server_port=(\\d+)-\\d+",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexTCPTransport = Pattern.compile("client_port=(\\d+)-\\d+;",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSessionWithTimeout = Pattern.compile("(\\S+);timeout=(\\d+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPgetTrack1 = Pattern.compile("trackID=(\\d+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPgetTrack2 = Pattern.compile("control:(\\S+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPmediadescript = Pattern.compile("m=(\\S+) .+",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPpacketizationMode = Pattern.compile("packetization-mode=(\\d);",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPspspps = Pattern.compile("sprop-parameter-sets=(\\S+),(\\S+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPlength = Pattern.compile("Content-length: (\\d+)",Pattern.CASE_INSENSITIVE);
//        public static final Pattern regexSDPstartFlag = Pattern.compile("v=(\\d)",Pattern.CASE_INSENSITIVE);
//
//        public int state;
//        public static HashMap<String,String> headers = new HashMap<>();
//
//        public static Response parseResponse(BufferedReader input) throws IOException  {
//            Response response = new Response();
//            String line;
//            Matcher matcher;
//            int sdpContentLength = 0;
//            if( (line = input.readLine()) == null) throw new IOException("Connection lost");
//            matcher = regexStatus.matcher(line);
//            if(matcher.find())
//                response.state = Integer.parseInt(matcher.group(1));
//            else
//                while ( (line = input.readLine()) != null ) {
//                    matcher = regexStatus.matcher(line);
//                    if(matcher.find()) {
//                        response.state = Integer.parseInt(matcher.group(1));
//                        break;
//                    }
//                }
//            Log.d(tag, "The response state is: "+response.state);
//
//            int foundMediaType = 0;
//            int sdpHaveReadLength = 0;
//            boolean sdpStartFlag = false;
//
//            while ( (line = input.readLine()) != null) {
//                if( line.length() > 3 || Describeflag ) {
//                    Log.d(tag, "The line is: " + line + "...");
//                    matcher = regexHeader.matcher(line);
//                    if (matcher.find())
//                        headers.put(matcher.group(1).toLowerCase(Locale.US), matcher.group(2)); //$ to $
//
//                    matcher = regexSDPlength.matcher(line);
//                    if(matcher.find()) {
//                        sdpContentLength = Integer.parseInt(matcher.group(1));
//                        sdpHaveReadLength = 0;
//                    }
//                    //Here is trying to get the SDP information from the describe response
//                    if (Describeflag) {
//                        matcher = regexSDPmediadescript.matcher(line);
//                        if (matcher.find())
//                            if (matcher.group(1).equalsIgnoreCase("audio")) {
//                                foundMediaType = 1;
//                                sdpInfo.audioTrackFlag = true;
//                            } else if (matcher.group(1).equalsIgnoreCase("video")) {
//                                foundMediaType = 2;
//                                sdpInfo.videoTrackFlag = true;
//                            }
//
//                        matcher = regexSDPpacketizationMode.matcher(line);
//                        if (matcher.find()) {
//                            sdpInfo.packetizationMode = Integer.parseInt(matcher.group(1));
//                        }
//
//                        matcher = regexSDPspspps.matcher(line);
//                        if(matcher.find()) {
//                            sdpInfo.SPS = matcher.group(1);
//                            sdpInfo.PPS = matcher.group(2);
//                        }
//
//                        matcher = regexSDPgetTrack1.matcher(line);
//                        if(matcher.find())
//                            if (foundMediaType == 1) sdpInfo.audioTrack = "trackID=" + matcher.group(1);
//                            else if (foundMediaType == 2) sdpInfo.videoTrack = "trackID=" + matcher.group(1);
//
//
//                        matcher = regexSDPgetTrack2.matcher(line);
//                        if(matcher.find())
//                            if (foundMediaType == 1) sdpInfo.audioTrack = matcher.group(1);
//                            else if (foundMediaType == 2) sdpInfo.videoTrack = matcher.group(1);
//
//
//                        matcher = regexSDPstartFlag.matcher(line);
//                        if(matcher.find()) sdpStartFlag = true;
//                        if(sdpStartFlag) sdpHaveReadLength += line.getBytes().length + 2;
//                        if((sdpContentLength < sdpHaveReadLength + 2) && (sdpContentLength != 0)) {
//                            Describeflag = false;
//                            sdpStartFlag = false;
//                            Log.d(tag, "The SDP info: "
//                                    + (sdpInfo.audioTrackFlag ? "have audio info.. " : "haven't the audio info.. ")
//                                    + ";" + (sdpInfo.audioTrackFlag ? (" the audio track is " + sdpInfo.audioTrack) : ""));
//                            Log.d(tag, "The SDP info: "
//                                    + (sdpInfo.videoTrackFlag ? "have video info.. " : "haven't the vedio info..")
//                                    + (sdpInfo.videoTrackFlag ? (" the video track is " + sdpInfo.videoTrack) : "")
//                                    + ";" + (sdpInfo.videoTrackFlag ? (" the video SPS is " + sdpInfo.SPS) : "")
//                                    + ";" + (sdpInfo.videoTrackFlag ? (" the video PPS is " + sdpInfo.PPS) : "")
//                                    + ";" + (sdpInfo.videoTrackFlag ? (" the video packetization mode is " + sdpInfo.packetizationMode) : ""));
//                            break;
//                        }
//                    }
//                } else {
//                    break;
//                }
//
//            }
//
//            if( line == null ) throw new IOException("Connection lost");
//
//            return  response;
//        }
//    }
//
//}
//
